package com.datastructure.test.longestcommonsubarry;

public class LongestCommonSubArray {

    public static void main(String[] args) {
        LongestCommonSubArray longestCommonSubArray = new LongestCommonSubArray();
        int[] A = {1,2};
        int[] B = {1,3,2};
        int i = longestCommonSubArray.longestCommonSubarry(A, B);
        System.out.println(i);
    }

    public int longestCommonSubarry (int[] A, int[] B) {
        // write code here
        int lengthOfA = A.length;
        int lengthOfB = B.length;
        int max = 0;
        int[][] temp = new int[lengthOfA +1][lengthOfB +1];
        for (int i = 1; i <= lengthOfA; i++) {
            for (int j = 1; j <= lengthOfB; j++) {
                if(A[i-1]==B[j-1]){
                    temp[i][j]=temp[i-1][j-1]+1;
                    max=Math.max(max,temp[i][j]);
                }
            }
        }
        return max;
    }
}
